package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"pet-track/cache"
	"pet-track/model"
	"pet-track/pkg"
	"strconv"
	"sync"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "不能为空",
		})
		return
	}
	var user model.User
	redis_key := "login_" + username
	jsData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if jsData >= 3 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "密码错误三次，请两个小时之后再来",
		})
		return
	}
	err := model.DB.Where("username=?", username).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "登录失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "用户不存在",
		})
		return
	}

	if user.Password != pkg.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if jsData == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 400,
				"msg":  "密码错误三次,禁封2小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登陆成功",
		"data": user,
	})
	return
}

func CreatePet(c *gin.Context) {
	name := c.PostForm("name")
	class := c.PostForm("class")
	Img := c.PostForm("img")
	if name == "" || class == "" || Img == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "不能为空",
		})
		return
	}
	data := model.Pet{
		Name:  name,
		Class: class,
		Img:   Img,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func ListPet(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	var pet []model.Pet
	redis_key := "list" + strconv.Itoa(page)
	JsonData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if JsonData == "" {
		model.DB.Offset(offset).Limit(size).Order("Created_at desc").Find(&pet)
		CacheData, _ := json.Marshal(pet)
		cache.Rdb.Set(cache.Ctx, redis_key, CacheData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(JsonData), &pet)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": pet,
	})
	return
}

func DeletePet(c *gin.Context) {
	id := c.Query("id")
	var pet model.Pet
	var once sync.Once
	once.Do(func() {
		model.DB.Where("id=?", id).Delete(&pet)

	})

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
	return
}

func UpdatePe(c *gin.Context) {
	name := c.PostForm("name")
	class := c.PostForm("class")
	Img := c.PostForm("img")
	id, _ := strconv.Atoi(c.PostForm("id"))
	if name == "" || class == "" || Img == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "不能为空",
		})
		return
	}
	var pet model.Pet
	var once sync.Once
	pet.Name = name
	pet.Class = class
	pet.ID = uint(id)
	pet.Img = Img
	once.Do(func() {
		model.DB.Where("id=?", id).Updates(&pet)

	})

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "修改成功",
		"data": pet,
	})
	return

}
